// pages/indexCenter/centerDetail/centerDetail.js
var api = require('../../../utils/api.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //弹出框是否显示
    modelShow:false,
    TaskId:'',
    PoninterCenterId:'',
    resNum:'',
    info:'',
    img0:'',
    img1:'',
    img2:'',
    img3:'',
    img4:'',
    img5:'',
    img6:'',
    Remarks:'',
    radinValue:'',
    status:0,
    addressShow:false,
    addressList:'',
    quesult:'',
    quesultShow:false,
    quesultList:'',
    quesultListShow:false
  },
  //添加备注
  addRemark(){
    this.setData({
      modelShow:true
    })
  },
  //点击了弹出框的取消按钮
  closeModel(e){
    let show = e.detail.show
    this.setData({
      modelShow:show
    })
  },
  //点击了修改按钮
  changeValue(e){
    let show = e.detail.show
    let value = e.detail.value
    this.setData({
      modelShow:show
    })
    this.setData({
      Remarks:value
    })
  },
  getChu(e){
    let num = e.currentTarget.dataset.index
    let resNum = this.data.resNum ? this.data.resNum : {} 
    resNum['value_'+num] = this.data.info.PointerResult[num].PointerResultId+'###'+e.detail.value
    this.setData({resNum})
    //data['value_'+num] = e.detail.value
    //console.log(this.data.resNum)
    console.log(e.detail.value)
  },
  //唤起相机
  getPhoto(){
    let _this = this  
    wx.chooseImage({
        count: 7, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
         // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          //console.log(tempFilePaths)
          for(let i=0;i<tempFilePaths.length;i++){
            let url = 'img'+i
            _this.setData({
              [url]:tempFilePaths[i]
            })
          }
          //console.log(_this.data)
         
       }
       })
  },
  upPhoto(e){
    let _this = this
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
         // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          //console.log(tempFilePaths)
          let img = 'img'+e.currentTarget.dataset.id
          _this.setData({
            [img]:tempFilePaths[0]
          })
       }
       })
       
  },

  radioChange(e){
    //console.log(e.detail.value)
    this.setData({
      radinValue:e.detail.value
    })
  },
  upResult(){
    //上传图片
    wx.showLoading({
      title: '正在提交',
    })
    let data ={
      TaskId:this.data.TaskId,
      PoninterCenterId:this.data.PoninterCenterId,
      Img:'',
      Remarks:this.data.Remarks,
      Value:''
    }
    if(this.data.resNum!=''){
      for(let item in this.data.resNum){
        //console.log(this.data.resNum[item])
        data.Value = this.data.resNum[item]+'@@@'+data.Value
      }
    }else if(this.data.info.OptionTypes == 0){
      for(let item in this.data.info.PointerResult){
        data.Value =this.data.info.PointerResult[item].PointerResultId+'###'+ this.data.info.PointerResult[item].Value+'@@@'+data.Value
        //console.log(data.Value)
      }
    }else{
      data.Value = this.data.radinValue
    }
    
    let imgList = []
    if(this.data.img0!=''){
      imgList.push(this.data.img0)
    }
     if(this.data.img1!=''){
      imgList.push(this.data.img1)
    }
     if(this.data.img2!=''){
      imgList.push(this.data.img2)
    }
    if(this.data.img3!=''){
      imgList.push(this.data.img3)
    }
     if(this.data.img4!=''){
      imgList.push(this.data.img4)
    }
     if(this.data.img5!=''){
      imgList.push(this.data.img5)
    }
    if(this.data.img6!=''){
      imgList.push(this.data.img6)
    }
    let temUrl = []
    //console.log(imgList)
    for(let i =0;i<imgList.length;i++){
      if(imgList[i].indexOf('tmp')==-1){
        data.Img=imgList[i]+'###'+data.Img
        temUrl.push(imgList[i])
      }else{
        api.upLoad(imgList[i]).then(res=>{
          //console.log(res)
          //res = decodeURIComponent(res);
          res = JSON.parse(res)
          temUrl.push(res.Data)
          data.Img=res.Data+'###'+data.Img
          //console.log(data)
        })
      }
    }
    let timestamp = Date.parse(new Date())
    //console.log(timestamp)
    let timeout = setInterval(function(){
      if(temUrl.length==imgList.length){
        console.log(data)
        api.SaveInspectInfo(data).then(res =>{
          //console.log(res)
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 1000
          })
          clearInterval(timeout)
        })
        
      }else{
        let nowtime = timestamp-Date.parse(new Date())
        if(nowtime>30){
          wx.showToast({
            title: '上传超时',
            icon: 'error',
            duration: 1000
          })
        }
      }
    },500)
  
  },
  //图片详细位置
  addAddress(){
    this.setData({
      addressShow:true
    })
  },
  closeinputModel(e){
    let show = e.detail.show
    this.setData({
      addressShow:show
    })
  },
  changeinputModel(e){
    let show = e.detail.show
    let value = e.detail.value
    this.setData({
      addressShow:show
    })
    //console.log(value)
    let data = {
      TaskId:this.data.TaskId,
      PoninterCenterId:this.data.info.PoninterCenterId,
      Address:''
    }
    for(let i =0;i<value.length;i++){
      if(value[i] == 0){
        data.Address = this.data.addressList[i]+data.Address+'###'
      }else{
        data.Address = data.Address+value[i]+'###'
      }
    }
    //console.log(data)
    let _this = this
    api.SaveInspectInfo(data).then(res=>{
      //console.log(res)
      _this.onLoad()
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1000
      })
    })
  },
  //问题描述选择
  addQuseult(){
    this.setData({
      quesultShow:true
    })
  },
  closeselectModel(e){
    let show = e.detail.show
    this.setData({
      quesultShow:show
    })
  },
  changeselectModel(e){
    let show = e.detail.show
    let value = e.detail.value
    let info = e.detail.text
    this.setData({
      quesultShow:show,
      quesult:info
    })
    //console.log(info)
    let data = {
      TaskId:this.data.TaskId,
      PoninterCenterId:this.data.info.PoninterCenterId,
      Description:''
    }
    for(let i =0;i<info.length;i++){
      if(info[i] == ""){
        data.Description = this.data.quesult[i]+data.Description+'###'
        //console.log(data.Description)
      }else{
        data.Description = data.Description+info[i]+'###'
      }
    }
    console.log(data)
    api.SaveInspectInfo(data).then(res=>{
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1000
      })
    })
  },
  //问题列表
  quesultLis(){
    this.setData({
      quesultListShow:true
    })
  },
  closequesultList(e){
    let show = e.detail.show
    this.setData({
      quesultListShow:show
    })
  },
  changequesultList(e){
    let show = e.detail.show
    this.setData({
      quesultListShow:show
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options)
    this.setData({
      TaskId:options.id,
      PoninterCenterId:options.PoninterCenterId,
      status:options.status
    })
    let data ={
      TaskId:this.data.TaskId,
      PoninterCenterId:this.data.PoninterCenterId,
    }
    // wx.showLoading({
    //   title: '正在加载',
    //   mask:true
    // })
    api.GetInspectInfo(data).then(res =>{
      for(let i=0;i<res.Data.TaskInspect.Img.length;i++){
        let img = 'img'+i
        this.setData({
          [img]:res.Data.TaskInspect.Img[i]
        })
        
      }
      if(res.Data.PointerResult[0].Value==undefined){
        res.Data.PointerResult[0].Value = 0
      }
      this.setData({
        info:res.Data,
        addressList:res.Data.TaskInspect.Address,
        quesult:res.Data.TaskInspect.Description
      })

      // wx.hideLoading({
      //   success: (res) => {},
      // })

    })    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    api.SearchProblem().then(res =>{
      //console.log(res)
      this.setData({
        quesultList:res.Data
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})